import store from '../index';
import type { ProjectConfig } from '/@/types/config';
import { getModule, Module, Action, Mutation, VuexModule } from 'vuex-module-decorators';
import { deepMerge } from '/@/utils';
import { getLocal, setLocal } from '/@/utils/helper/persistent';
import { PROJ_CFG_KEY } from '/@/enums/cacheEnum';

const NAME = 'app';
@Module({ dynamic: true, namespaced: true, store, name: NAME })
class App extends VuexModule {
  private projectConfigSate: ProjectConfig | null = getLocal(PROJ_CFG_KEY);

  get getProjectConfig(): ProjectConfig {
    return this.projectConfigSate || ({} as ProjectConfig);
  }

  @Mutation
  commitProjectConfigState(proCfg: ProjectConfig): void {
    this.projectConfigSate = deepMerge(this.projectConfigSate || {}, proCfg);
    setLocal(PROJ_CFG_KEY, this.projectConfigSate);
  }
}

export const appStore = getModule(App);
